package com.wzx.ptplatform.controller.admin.company;

import com.wzx.ptplatform.domain.*;
import com.wzx.ptplatform.service.PartTimeMessageService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员企业模块——兼职信息管理")
@RestController
public class AdminCompanyPtPublishedController {

    final PartTimeMessageService partTimeMessageService;

    public AdminCompanyPtPublishedController(PartTimeMessageService partTimeMessageService) {
        this.partTimeMessageService = partTimeMessageService;
    }

    @ApiOperation(value = "查询已发布兼职信息列表")
    @RequestMapping("admin_company_pt_published_message_list")
    public LayuiPager<PartTimeMessage> adminCompanyPtPublishedMessageList(
            String companyName, String jobName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        PartTimeMessage partTimeMessage = new PartTimeMessage();
        if (companyName != null && !"".equals(companyName)) {
            partTimeMessage.setCompany(new Company(companyName));
        }
        if (jobName != null && !ConstUtils.PLEASE_CHOOSE.equals(jobName)) {
            partTimeMessage.setJob(new Job(Long.parseLong(jobName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            partTimeMessage.setCity(new City(Long.parseLong(cityName)));
        }
        Page<PartTimeMessage> pager = partTimeMessageService.findList(partTimeMessage, page - 1, limit);
        LayuiPager<PartTimeMessage> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @ApiOperation(value = "查询未发布兼职信息列表")
    @RequestMapping("admin_company_pt_unpublished_message_list")
    public LayuiPager<PartTimeMessage> adminCompanyPtUnpublishedMessageList(
            String companyName, String jobName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        PartTimeMessage partTimeMessage = new PartTimeMessage();
        if (companyName != null && !"".equals(companyName)) {
            partTimeMessage.setCompany(new Company(companyName));
        }
        if (jobName != null && !ConstUtils.PLEASE_CHOOSE.equals(jobName)) {
            partTimeMessage.setJob(new Job(Long.parseLong(jobName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            partTimeMessage.setCity(new City(Long.parseLong(cityName)));
        }
        Page<PartTimeMessage> pager = partTimeMessageService.findUnList(partTimeMessage, page - 1, limit);
        LayuiPager<PartTimeMessage> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @ApiOperation(value = "跳转修改页面")
    @GetMapping("admin_company_pt_published_message_editView")
    public ModelAndView adminCompanyPtPublishedMessageEditView(String id, Model model) {
        model.addAttribute("partTimeMessage", partTimeMessageService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/company/pt_published_message_edit");
    }

    @ApiOperation(value = "保存")
    @PostMapping("admin_company_pt_published_message_save_or_edit")
    public Map<String, Object> adminCompanyPtPublishedMessageSaveOrEdit(PartTimeMessage partTimeMessage) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (partTimeMessageService.saveOrEdit(partTimeMessage)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @ApiOperation(value = "删除")
    @PostMapping("admin_company_pt_published_message_delete")
    public Map<String, Object> adminCompanyPtPublishedMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (partTimeMessageService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("admin_company_pt_published_message_delete_select")
    public Map<String, Object> adminCompanyPtPublishedMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    partTimeMessageService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
